<?php
namespace app\restaurant\controller;

use app\ApiRest;
use app\dynamic\model\CardUser;
use app\dynamic\model\UserInfo;
use app\restaurant\model\Goods;
use app\restaurant\model\StoreStaff;
use think\App;

use app\restaurant\model\Store as model;

use app\restaurant\model\Order;

use app\restaurant\model\RefundOrder;

use think\facade\Db;


class IndexStore extends ApiRest
{

    protected $model;

    protected $staff_model;

    protected $order_model;

    protected $refund_model;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new Model();

        $this->staff_model = new StoreStaff();

        $this->order_model = new Order();

        $this->refund_model = new RefundOrder();

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:35
     * @功能说明:列表
     */
    public function storeList(){

        $input = $this->_param;

        $dis[] = ['status','>',-1];

        $dis[] = ['uniacid','=',$this->_uniacid];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        $lat = !empty($input['latitude'])?$input['latitude']:0;

        $lng = !empty($input['long'])?$input['long']:0;

        $alh = '(2 * 6378.137* ASIN(SQRT(POW(SIN(PI()*('.$lng.'- `long`)/360),2)+COS(PI()*33.07078170776367/180)* COS('.$lat.' * PI()/180)*POW(SIN(PI()*('.$lat.'- latitude)/360),2))))*1000 as distance';

        $data = $this->model->indexStoreList($dis,$alh);


        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-16 10:55
     * @功能说明:选择门店
     */
    public function selectStore(){

        $input = $this->_input;

        $res = Db::name('longbing_card_user')->where(['id'=>$this->getUserId()])->update(['store_id'=>$input['id']]);

        $user = $this->getUserInfo();

        $user['store_id'] = $input['id'];

        $key = 'longbing_card_user_' . $this->getUserId();

        setCache($key,$user,1800,$this->_uniacid);

        setCache($this->autograph,$user,1800,$this->_uniacid);
//        delCache($this->autograph, $this->_uniacid);
        return $this->success($res);

    }

    /**
     * @author chenniang
     * @DataTime: 2020-10-16 09:18
     * @功能说明:
     */
    public function storeInfo(){

        $input = $this->_param;

        $lat = !empty($input['latitude'])?$input['latitude']:0;

        $lng = !empty($input['long'])?$input['long']:0;

        $alh = '(2 * 6378.137* ASIN(SQRT(POW(SIN(PI()*('.$lng.'- `long`)/360),2)+COS(PI()*33.07078170776367/180)* COS('.$lat.' * PI()/180)*POW(SIN(PI()*('.$lat.'- latitude)/360),2))))*1000 as distance';

        $data= $this->model->where(['id'=>$input['id']])->field(['*',$alh])->find();

        $data= !empty($data)?$data->toArray():[];

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-22 13:24
     * @功能说明:
     */
    public function myStoreInfo(){

        $input = $this->_param;

        $user_info = $this->getUserInfo();
        //选择桌号
        if(!empty($input['table_id'])){

            $user_info = $this->model->selectTable($this->getUserId(),$input['table_id'],$user_info);

            if(!empty($user_info['code'])){

                $this->errorMsg($user_info['msg']);
            }
        }

        if(empty($user_info['store_id'])){

            $this->errorMsg('请选择门店','302');

        }

        $lat = !empty($input['latitude'])?$input['latitude']:0;

        $lng = !empty($input['long'])?$input['long']:0;

        $alh = '(2 * 6378.137* ASIN(SQRT(POW(SIN(PI()*('.$lng.'- `long`)/360),2)+COS(PI()*33.07078170776367/180)* COS('.$lat.' * PI()/180)*POW(SIN(PI()*('.$lat.'- latitude)/360),2))))*1000 as distance';

        $data= $this->model->where(['id'=>$user_info['store_id']])->field(['*',$alh])->find();

        $data= !empty($data)?$data->toArray():[];

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-05 10:54
     * @功能说明:判断是管理员还是员工
     */
    public function checkRole(){

        $staff_model = new StoreStaff();

        $store = $staff_model->getStaffStore($this->getUserId(),3);

        if(empty($store)){

            return $this->success(0);
        }

        if($store['role']==0){

            return $this->success(1);

        }

        if($store['role']==1){

            return $this->success(2);

        }

    }









}
